package com.dp.mallchat.common.chat.controller;


import com.dp.mallchat.common.chat.domain.vo.req.*;
import com.dp.mallchat.common.chat.domain.vo.resp.ChatMessageResp;
import com.dp.mallchat.common.chat.domain.vo.resp.MsgReadInfoResp;
import com.dp.mallchat.common.chat.domain.vo.resp.MsgReadPageResp;
import com.dp.mallchat.common.chat.service.ChatService;
import com.dp.mallchat.common.chat.service.MessageService;
import com.dp.mallchat.common.common.domain.vo.resp.ApiResult;
import com.dp.mallchat.common.common.domain.vo.resp.CursorPageBaseResp;
import com.dp.mallchat.common.common.utils.RequestHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.validation.Valid;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author <a href="https://gitee.com/baiDP">adu</a>
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/capi/chat")
@Api(value = "聊天室相关接口", tags = "聊天室相关接口")
public class MessageController {

    @Autowired
    private ChatService chatService;
    @Autowired
    private MessageService messageService;

    @ApiOperation(value = "发送消息")
    @PostMapping("/msg")
    public ApiResult<ChatMessageResp> sendMessage(@Valid @RequestBody SendMessageReq req){
        //发送消息
        Long msgId = messageService.sendMessage(RequestHolder.getThreadLocal().getUid(), req);
        //返回消息完成内容，更快速响应客户端
        return ApiResult.success(chatService.getMsgRespApi(msgId, RequestHolder.getThreadLocal().getUid()));
    }

    @ApiOperation(value = "游标翻页")
    @GetMapping("/public/msg/page")
    public ApiResult<CursorPageBaseResp<ChatMessageResp>> page(@Valid ContractMsgPageReq req){
        CursorPageBaseResp<ChatMessageResp> cursorPageBaseResp = chatService.getContactMsgPage(req, RequestHolder.getThreadLocal().getUid());
        return ApiResult.success(cursorPageBaseResp);
    }

    @ApiOperation(value = "撤回消息")
    @PutMapping("/msg/recall")
    public ApiResult<Void> recall(@Valid @RequestBody ReCallReq req) {
        chatService.recall(req, RequestHolder.getThreadLocal().getUid());
        return ApiResult.success();
    }


    @GetMapping("/msg/read")
    @ApiOperation("获取消息的已读未读总数")
    public ApiResult<Collection<MsgReadInfoResp>> getReadInfo(@Valid ChatMessageReadInfoReq request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        return ApiResult.success(chatService.getMsgReadInfo(uid, request));
    }

    @PutMapping("/msg/read")
    @ApiOperation("消息阅读上报")
    public ApiResult<Void> msgRead(@Valid @RequestBody ChatMessageMemberReq request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        chatService.msgRead(uid, request);
        return ApiResult.success();
    }

    @PutMapping("/msg/mark")
    @ApiOperation("消息标记")
    public ApiResult<Void> msgMark(@Valid @RequestBody ChatMessageMarkReq request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        chatService.msgMark(uid, request);
        return ApiResult.success();
    }

    @GetMapping("/msg/read/page")
    @ApiOperation("消息已读未读列表")
    public ApiResult<CursorPageBaseResp<MsgReadPageResp>> msgReadPage(@Valid MsgReadPageReq request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        return ApiResult.success(chatService.msgReadPage(uid, request));
    }
}

